package configtools

import (
	log "github.com/sirupsen/logrus"
	"github.com/spf13/viper"
	"strings"
)

var GlobalConfiguration *viper.Viper
var DatabaseConfiguration *viper.Viper

func init() {
	GlobalConfiguration = viper.New()
	DatabaseConfiguration = viper.New()
	GlobalConfiguration.SetConfigName("application")
	GlobalConfiguration.AddConfigPath(".")
	GlobalConfiguration.AddConfigPath("../") // 对于测试文件，需要回到cybertwin目录下
	GlobalConfiguration.SetConfigType("yaml")
	if err := GlobalConfiguration.ReadInConfig(); err != nil {
		log.Errorln("[init]-无法找到application.yaml配置文件" + err.Error())
		return
	}
	DatabaseConfiguration.SetConfigName("pgconfig")
	DatabaseConfiguration.AddConfigPath("/config")
	DatabaseConfiguration.SetConfigType("yaml")
	if err := DatabaseConfiguration.ReadInConfig(); err != nil {
		log.Infoln("[init]-无法找到/config/pgconfig.yaml配置文件:" + err.Error())
		return
	}
}

func GetRegisterPublicKeyFile() (dirLocation string, publicKeyFileNameWithSuffix string) {
	dirLocation = GlobalConfiguration.GetString("crypt.registerCenter.dirLocation")
	publicKeyFileNameWithSuffix = GlobalConfiguration.GetString("crypt.registerCenter.pkName")
	dirLocation, _ = strings.CutSuffix(dirLocation, "/")
	return
}
